<#include 'common/java.ftl'>
<#include 'common/swagger.ftl'>
<@commonJavaFileHeader  className=classInfo.className+"ServiceImpl" classComment=classInfo.classComment+"ServiceImpl" />

package ${packageName}.${subPackageName};

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.win.dfas.common.entity.BaseEntity;
import com.win.dfas.common.util.ObjectUtils;
import com.win.dfas.common.util.UserUtil;
import com.win.dfas.common.util.WinExceptionUtil;
import com.win.dfas.common.exception.WinException;
import com.win.dfas.common.enumeration.StatusEnum.AuditStatusEnum;
import com.win.dfbp.common.log.BusinessLog;
import com.win.dfbp.common.log.BusinessParam;
import com.win.dfbp.common.log.handle.ImportHandle;
import com.win.dfbp.common.log.handle.QueryHandle;
import com.win.dfbp.common.log.handle.SimpleHandle;
import com.win.dfbp.common.log.handle.UpdateHandle;
import com.win.dfbp.common.log.util.BusinessLogConstant;
import com.win.dfbp.common.log.util.DataCache;
import com.win.dfbp.common.log.util.ParamType;
import com.win.dfas.common.util.CollectionUtil;
import ${packageName}.entity.${classInfo.className};
import ${packageName}.service.${classInfo.className}Service;
import ${packageName}.dao.${classInfo.className}Mapper;
import ${packageName}.vo.request.${classInfo.className}AddReqVO;
import ${packageName}.vo.request.${classInfo.className}UpdateReqVO;
import ${packageName}.vo.request.${classInfo.className}QryReqVO;
import ${packageName}.vo.response.${classInfo.className}RepVO;

import java.util.List;
import java.util.Objects;

<@commonJavaClassCommnet subPackageName=subPackageName  className=classInfo.className+"ServiceImpl" classComment=classInfo.classComment+"ServiceImpl" />
@Service
@Transactional(rollbackFor = Exception.class)
public class ${classInfo.className}ServiceImpl implements ${classInfo.className}Service {

	@Autowired
	private ${classInfo.className}Mapper ${classInfo.className?uncap_first}Mapper;

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.ADD, businessType = BUSINESS_TYPE,
			tableName = TABLE_NAME, description = "点击新增按钮，新增${classInfo.classComment}信息")
	public int save(${classInfo.className}AddReqVO reqVO) {
		${classInfo.className} ${classInfo.className?uncap_first} = new ${classInfo.className}();
		BeanUtils.copyProperties(reqVO, ${classInfo.className?uncap_first});
		${classInfo.className?uncap_first}.preSave();
		// 数据id
		DataCache.addCache(BusinessLogConstant.BUSINESS_ID, ${classInfo.className?uncap_first}.getId());
		return ${classInfo.className?uncap_first}Mapper.save(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.ADD, businessType = BUSINESS_TYPE,
			tableName = TABLE_NAME, description = "点击新增按钮，新增${classInfo.classComment}信息")
	public int saveList(List<${classInfo.className}AddReqVO> entityList) {
		List<${classInfo.className}> ${classInfo.className?uncap_first} = ObjectUtils.copyPropertiesList(entityList, ${classInfo.className}.class, false);
		${classInfo.className?uncap_first}.forEach(BaseEntity::preSave);
		DataCache.addCache(BusinessLogConstant.BUSINESS_ID, CollectionUtil.mapFieldObject(${classInfo.className?uncap_first}, ${classInfo.className}::getId));
		return ${classInfo.className?uncap_first}Mapper.saveList(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.UPDATE, businessType = BUSINESS_TYPE,
			handle = UpdateHandle.class, param = @BusinessParam(name = "reqVO"),
			tableName = TABLE_NAME, description = "点击修改按钮，修改${classInfo.classComment}信息")
	public int updateById(${classInfo.className}UpdateReqVO reqVO) {
		// 查询数据库旧数据
		${classInfo.className} old${classInfo.className} = ${classInfo.className?uncap_first}Mapper.getById(reqVO.getId());
		if(old${classInfo.className} == null) {
			throw new WinException("id不存在");
		}
		DataCache.addCache(BusinessLogConstant.UPDATE_OLD_DATE_KEY, old${classInfo.className});

		${classInfo.className} ${classInfo.className?uncap_first} = new ${classInfo.className}();
		BeanUtils.copyProperties(reqVO, ${classInfo.className?uncap_first});
		${classInfo.className?uncap_first}.preUpdate();
		return ${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.UPDATE, businessType = BUSINESS_TYPE,
			handle = UpdateHandle.class, param = @BusinessParam(name = "entityList", type = ParamType.COLLECTION),
			tableName = TABLE_NAME, description = "点击修改按钮，修改${classInfo.classComment}信息")
	public int updateByIds(List<${classInfo.className}UpdateReqVO> entityList) {
		// 获取老数据
		List<Long> ids = CollectionUtil.mapFieldObject(entityList, ${classInfo.className}UpdateReqVO::getId);
		List<${classInfo.className}> old${classInfo.className}List = ${classInfo.className?uncap_first}Mapper.getByIds(ids);
		if (ids.size() != old${classInfo.className}List.size()) {
			throw new WinException("修改的数据条数和数据库中的条数不匹配");
		}
		DataCache.addCache(BusinessLogConstant.UPDATE_OLD_DATE_KEY, old${classInfo.className}List);

		List<${classInfo.className}> ${classInfo.className?uncap_first} = ObjectUtils.copyPropertiesList(entityList, ${classInfo.className}.class, false);
		${classInfo.className?uncap_first}.forEach(BaseEntity::preUpdate);
		return ${classInfo.className?uncap_first}Mapper.updateByIds(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.DELETE, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = BusinessLogConstant.BUSINESS_ID, value = "#id"),
			tableName = TABLE_NAME, description = "点击删除按钮，删除${classInfo.classComment}信息")
	public int deleteById(Long id) {
		${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Mapper.getById(id);
		if(ObjectUtil.isNull(${classInfo.className?uncap_first})) {
			throw new WinException("id不存在");
		}
		${classInfo.className?uncap_first}.preDelete();
		return ${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.DELETE, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = BusinessLogConstant.BUSINESS_ID, value = "#ids", type = ParamType.COLLECTION),
			tableName = TABLE_NAME, description = "点击删除按钮，删除${classInfo.classComment}信息")
	public int deleteByIds(List<Long> ids) {
		List<${classInfo.className}> ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Mapper.getByIds(ids);
		if(CollUtil.isEmpty(${classInfo.className?uncap_first})) {
			throw new WinException("id不存在");
		}
		${classInfo.className?uncap_first}.forEach(BaseEntity::preDelete);
		return ${classInfo.className?uncap_first}Mapper.updateByIds(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.QUERY, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = BusinessLogConstant.BUSINESS_ID, value = "#id"),
			tableName = TABLE_NAME, description = "菜单【{#menuName}】，点击查询按钮，按照id={#id}查询${classInfo.classComment}信息")
	public ${classInfo.className}RepVO getById(Long id) {
		${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Mapper.getById(id);
		if(ObjectUtil.isNull(${classInfo.className?uncap_first})) {
			return null;
		}
		${classInfo.className}RepVO ${classInfo.className?uncap_first}RepVO =  new ${classInfo.className}RepVO();
		BeanUtils.copyProperties(${classInfo.className?uncap_first}, ${classInfo.className?uncap_first}RepVO);
		return ${classInfo.className?uncap_first}RepVO;
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.QUERY, businessType = BUSINESS_TYPE, handle = SimpleHandle.class,
			tableName = TABLE_NAME, description = "菜单【{#menuName}】，点击查询按钮，按照id={#ids}查询${classInfo.classComment}信息")
	public List<${classInfo.className}RepVO> getByIds(List<Long> ids) {
		List<${classInfo.className}> ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Mapper.getByIds(ids);
		if(ObjectUtil.isNull(${classInfo.className?uncap_first})) {
			return null;
		}
		return ObjectUtils.copyPropertiesList(${classInfo.className?uncap_first}, ${classInfo.className}RepVO.class, true);
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.QUERY, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = "reqVO"), handle = QueryHandle.class,
			tableName = TABLE_NAME, description = "菜单【{#menuName}】，点击列表查询按钮，列表查询${classInfo.classComment}信息")
	public List<${classInfo.className}RepVO> list(${classInfo.className}QryReqVO reqVO) {
		List<${classInfo.className}> list = ${classInfo.className?uncap_first}Mapper.list(reqVO);
		return ObjectUtils.copyPropertiesList(list, ${classInfo.className}RepVO.class, true);
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.QUERY, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = "reqVO"), handle = QueryHandle.class,
			tableName = TABLE_NAME, description = "菜单【{#menuName}】，点击分页查询按钮，分页查询${classInfo.classComment}信息")
	public PageInfo<${classInfo.className}RepVO> pageList(${classInfo.className}QryReqVO reqVO) {
		PageHelper.startPage(reqVO.getReqPageNum(), reqVO.getReqPageSize());
		List<${classInfo.className}> list = ${classInfo.className?uncap_first}Mapper.list(reqVO);
		PageInfo<${classInfo.className}> pageInfo = new PageInfo<>(list);
		return ObjectUtils.copyPageInfo(pageInfo, ${classInfo.className}RepVO.class,true);
	}

	<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0><#list classInfo.fieldList as fieldItem ><#if fieldItem.fieldName == "auditStatus">
	@Override
	@BusinessLog(operation = BusinLogTypeEnum.APPROVED, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = BusinessLogConstant.BUSINESS_ID, value = "#id"),
			tableName = TABLE_NAME, description = "点击审核按钮，审核${classInfo.classComment}信息")
	public int updateAuditById(Long id) {
		${classInfo.className} ${classInfo.className?uncap_first} = new ${classInfo.className}();
		${classInfo.className?uncap_first}.setId(id);
		${classInfo.className?uncap_first}.setAuditStatus(AuditStatusEnum.AUDITED.getStatusCode());
		${classInfo.className?uncap_first}.setAuditTime(DateUtil.date());
		${classInfo.className?uncap_first}.setAuditUserId(UserUtil.getUserId());
		return ${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first});
	}

	@Override
	@BusinessLog(operation = BusinLogTypeEnum.UNCHECK, businessType = BUSINESS_TYPE,
			param = @BusinessParam(name = BusinessLogConstant.BUSINESS_ID, value = "#id"),
			tableName = TABLE_NAME, description = "点击反审核按钮，反审核${classInfo.classComment}信息")
	public int updateRevertAuditById(Long id) {
		${classInfo.className} ${classInfo.className?uncap_first} = new ${classInfo.className}();
		${classInfo.className?uncap_first}.setId(id);
		${classInfo.className?uncap_first}.setAuditStatus(AuditStatusEnum.NON_AUDITED.getStatusCode());
		// TODO 反审核时可能需要检查数据是否正在其他关联表中使用
        ${classInfo.className?uncap_first}.setAuditTime(DateUtil.date());
        ${classInfo.className?uncap_first}.setAuditUserId(UserUtil.getUserId());
		return ${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first});
	}
		<#break>
	</#if>
	</#list>
	</#if>

}
